package com.demo.service.impl;

import com.demo.dto.DepartmentDTO;
import com.demo.dto.DepartmentPageQueryDTO;
import com.demo.mapper.DepartmentMapper;
import com.demo.pojo.Department;
import com.demo.pojo.User;
import com.demo.result.PageResult;
import com.demo.result.Result;
import com.demo.service.DepartmentService;
import com.demo.vo.DepartmentVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    //新增二级学院信息
    public void save(DepartmentDTO departmentDTO) {
        Department department = new Department();
        BeanUtils.copyProperties(departmentDTO, department);
        System.out.println(TransactionSynchronizationManager.isActualTransactionActive()+"11");
        departmentMapper.addDepartment(department);
        System.out.println(TransactionSynchronizationManager.isActualTransactionActive()+"22");
    }

    //分页查询
    public PageResult pageQuery(DepartmentPageQueryDTO departmentPageQueryDTO) {
        PageHelper.startPage(departmentPageQueryDTO.getPage(), departmentPageQueryDTO.getPageSize());
        Page<DepartmentVO> page = departmentMapper.pageQuery(departmentPageQueryDTO);
        long total = page.getTotal();
        List<DepartmentVO> records = page.getResult();
        return new PageResult(total, records);
    }

    @Override
    public void delete(String[] depIDs) {
        for (String depID : depIDs) {
            System.out.println(depID);
            departmentMapper.delete(depID);
        }

    }

    //删除二级学院信息
    public void deleteById(String depID){
        departmentMapper.delete(depID);
    }

    //修改二级学院信息
    public void update(DepartmentDTO departmentDTO){
        departmentMapper.update(departmentDTO);
    }
}
